package configuration;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.*;

/*
 * this class implemets a write and read operators on xml file
 */
public class ConfigOperator {
	/**
	 * write the data in Config class instance to an xml file
	 * @param f instance of config
	 * @param filename the name of the file we want to create
	 * @throws Exception
	 */
	 public static void write(Config f, String filename){
	        XMLEncoder encoder;
			try {
				encoder = new XMLEncoder(
				      new BufferedOutputStream(
				        new FileOutputStream(filename)));
			
	        encoder.writeObject(f);
	        encoder.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	 /**
	  * read the data from xml file and put it on Config class instance
	  * @param filename the name of file we want to read from
	  * @return the Config instance with the data from the file
	  * @throws Exception
	  */
	    public static Config read(String filename){
	    	Config o = null;
	    	XMLDecoder decoder;
			try {
				decoder = new XMLDecoder(new BufferedInputStream(
	            		new FileInputStream(filename)));
			
		        o = (Config)decoder.readObject();
		        decoder.close();
			}catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        return o;
	    }

}
